package com.riku.security.config;

import com.riku.security.core.SeAuthorityFilter;
import com.riku.security.core.SeAuthorityService;
import com.riku.security.model.SeApiRole;
import com.riku.security.model.SeMenu;
import com.riku.security.service.SeCommonService;
import com.riku.security.service.SeMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created by NANC on 2021/7/11.
 */
@Configuration
@EnableScheduling
@ConditionalOnBean(SeAuthorityService.class)
public class SeSyncronizeConfig {

    @Autowired
    private SeCommonService seCommonService;

    @Autowired
    private SeAuthorityService seAuthorityService;

    /**
     * 每半小时将所有接口刷新进缓存
     */
    @Scheduled(cron = "0 15 * * * ?")
    public void syncronizeSchedule() {
        Map<String,List<SeApiRole>> rootPaths = SeAuthorityFilter.getRootPaths();
        String[] codes = seAuthorityService.buildServicePath();
        if (codes == null) {
            return;
        }
        for (String code : codes) {
            rootPaths.put(code,seCommonService.getApiAndRole(code));
        }
    }
}
